﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Mogubang_Sl.Core;
using Mogubang_Sl.Utils;
using OpenXLive;
using Microsoft.Phone.Tasks;
using System.Windows.Threading;

namespace Mogubang_Sl
{
    public partial class MainPage : PhoneApplicationPage
    {
        private static bool isShowOpenAd = false;

        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= MainPage_Loaded;
            this.Storyboard1.RepeatBehavior = RepeatBehavior.Forever;
            this.Storyboard2.RepeatBehavior = RepeatBehavior.Forever;
            this.Storyboard3.RepeatBehavior = RepeatBehavior.Forever;
            this.Storyboard4.RepeatBehavior = RepeatBehavior.Forever;
            this.Storyboard5.RepeatBehavior = RepeatBehavior.Forever;
            this.Storyboard6.RepeatBehavior = RepeatBehavior.Forever;

            this.Storyboard1.Begin();
            this.Storyboard2.Begin();
            this.Storyboard3.Begin();
            this.Storyboard4.Begin();
            this.Storyboard5.Begin();
            this.Storyboard6.Begin();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (isOnGift)
            {
                this.StoryboardHidePackage.Begin();
                isOnGift = false;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //清除页面堆栈
            try
            {
                for (int i = 0; i < 4; i++)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            catch
            { }

            if (StageManager.Instance.LastStage != null && StageManager.Instance.LastStage.moguDatas != null)
            {
                this.btnContinue.IsEnabled = true;
            }
            else
            {
                this.btnContinue.IsEnabled = false;
            }

            this.txtHighScore.Text = StageManager.Instance.HighScore.ToString();

            if (StageManager.Instance.IsDoNewUserTask)
            {
                this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
            }
            //else if (UmengSDK.UmengAnalytics.GetOnlineParam("useGift") == "1" && StageManager.Instance.LaunchCount >= 3)
            else
            {
                this.btnGift.Visibility = System.Windows.Visibility.Visible;
            }

            if (StageManager.Instance.IsPlayBackground)
            {
                AudioPlayHelper.PlayBackground();
            }

            String usemokaStr = UmengSDK.UmengAnalytics.GetOnlineParam("usemoka");
            //0-关闭 1-同时开启 2-开启banner 3-开启开屏
            if ((usemokaStr == "1" || usemokaStr == "3") && !isShowOpenAd)
            {
                isShowOpenAd = true;
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += (object ss, EventArgs ee) =>
                {
                    timer.Stop();
                    string dateStr = DateTime.Now.ToString("yyyyMMdd") + (DateTime.Now.Hour < 12 ? "A" : "P");
                    if (StageManager.Instance.LastFullScreenDate != dateStr)
                    {
                        StageManager.Instance.LastFullScreenDate = dateStr;
                        NavigationService.Navigate(new Uri("/Game77SharedLib;component/MokaPage.xaml?channel=ppq", UriKind.RelativeOrAbsolute));
                    }
                };
                timer.Start();

            }
        }

        private void btnStartNewGame_Click(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickbutton);
            bool isStart = true;
            if (StageManager.Instance.LastStage != null && StageManager.Instance.LastStage.moguDatas != null)
            {
                isStart = MessageBox.Show("开始新游戏会丢失上一次的游戏记录，确定开始新游戏吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK;
            }
            if (isStart)
            {
                //跳转到游戏页面
                this.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml?isNewGame=1", UriKind.RelativeOrAbsolute));
            }
        }

        private void btnContinue_Click(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickbutton);
            if (StageManager.Instance.LastStage != null && StageManager.Instance.LastStage.moguDatas != null)
            {
                //跳转到游戏页面
                this.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml?isNewGame=0", UriKind.RelativeOrAbsolute));
            }
        }

        private void btnConfig_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickbutton);
            //跳转到设置页面
            this.NavigationService.Navigate(new Uri("/Pages/ConfigPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void btnHelp_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickbutton);
            //跳转到帮助页面
            this.NavigationService.Navigate(new Uri("/Pages/GuidePage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void OpenOpenXlive()
        {
            SoundManager.PlayEffect(SoundManager.clickbutton);
            if (XLiveGameManager.CurrentSession != null && XLiveGameManager.CurrentSession.IsValid)
            {
                OpenXLive.Silverlight.XLiveUIManager.ShowGameCenter();
            }
        }

        private void btnRank_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenOpenXlive();
        }

        #region 送金币

        bool isOnGift = false;

        private void btnGetMB_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickbutton);
            MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();
            StageManager.Instance.IsCommented = true;
            StageManager.Instance.IsDoNewUserTask = true;

            //增加5000
            StageManager.Instance.TotalG += 5000;

            this.btnGetMB.Content = "已获得";
            this.btnGetMB.IsEnabled = false;
            this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void btnCloseGift_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickbutton);
            isOnGift = false;
            this.StoryboardHidePackage.Begin();
        }

        private void btnGift_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickbutton);
            isOnGift = true;
            this.StoryboardShowPackage.Begin();
        }



        #endregion

        private void btn77game_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            UmengSDK.UmengAnalytics.TrackEvent("more77game");

            MarketplaceSearchTask task = new MarketplaceSearchTask();
            task.ContentType = MarketplaceContentType.Applications;
            task.SearchTerms = "77Studio";
            task.Show();

            //WebBrowserTask webBrowserTask = new WebBrowserTask();

            //webBrowserTask.Uri = new Uri("http://www.windowsphone.com/zh-CN/store/publishers?publisherId=77Studio&appId=eb56a57d-19ac-4286-9194-0c588339b274", UriKind.Absolute);

            //webBrowserTask.Show();
        }
    }
}